/**
 * @Author 天下无敌
 * @Date 怎么方便怎么来，懒的搞json
 * @AIDE AIDE+ 
 */
package com.ecs.core;
import com.artemis.World;
import com.artemis.Entity;
import com.artemis.Component;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.ecs.core.components.map.TerrainCom;
import com.ecs.core.components.map.ChunkTag;
import com.ecs.core.components.attribute.MovementComp;
import com.ecs.core.components.StateCom;
import com.ecs.core.components.draw.AnimationCom;
import com.ecs.core.utils.JsonAnimation;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.ecs.core.utils.AnimationType;
import com.ecs.core.components.map.ChunkPosCom;
import com.ecs.core.components.map.ChunkCom;
import com.ecs.core.components.draw.SpriteRender;
import com.ecs.core.components.PendingDelete;
import com.ecs.core.components.player.PlayerComp;
import com.ecs.core.components.attribute.Transform;
import com.ecs.core.other.state.IEntityState;
import com.ecs.core.components.map.TreeCom;

public class EntityBuilder{
	
	private static final EntityBuilder builder = new EntityBuilder();
	
	private Entity entity;
	
	public static EntityBuilder init(World world){
		builder.entity = world.createEntity();
		return builder;
	}
	public static EntityBuilder init(World world,int Id){
		builder.entity = world.getEntity(Id);
		return builder;
	}
	//Tree
	public EntityBuilder Tree(int tid){
		TreeCom tree = getCom(TreeCom.class);
		tree.tree_d =tid;
		return this;
	}
	//terrain
	public EntityBuilder Terrain(int id,boolean isWater){
		TerrainCom t = getCom(TerrainCom.class);
		t.terrainId = id;
		t.isWater = isWater;
		return this;
	}
	//chunkTag+
	public EntityBuilder ChunkTag(int chunk){
		ChunkTag t = getCom(ChunkTag.class);
		t.chunkId = chunk;
		return this;
	}
	//movement
	public EntityBuilder Movement(float speed){
		MovementComp m = getCom(MovementComp.class);
		m.moveSpeed = speed;
		return this;
	}
	//state
	public EntityBuilder EntityState(IEntityState state){
		StateCom s = getCom(StateCom.class);
		s.currentState = state;
		return this;
	}
	//animation
	public EntityBuilder Animation(String jsonPath,AssetManager asset){
		return Animation(JsonAnimation.loadAnimations(jsonPath,asset),AnimationType.IDLEDOWN);
	}
	public EntityBuilder Animation(ObjectMap<String,Animation<TextureRegion>> ans,String currentAnimation){
		AnimationCom a = getCom(AnimationCom.class);
		a.animations  = ans;
		a.currentAnimation = currentAnimation;
		return this;
	}
	//chunkPos
	public EntityBuilder ChunkPos(int x,int y){
		ChunkPosCom c = getCom(ChunkPosCom.class);
		c.chunkX = x;
		c.chunkY = y;
		return this;
	}
	//chunk
	public EntityBuilder Chunk(){
		getCom(ChunkCom.class);
		return this;
	}
	
	//spriteRender
	public EntityBuilder Sprite(TextureRegion region,int sortingOrder,ShaderProgram shader){
		SpriteRender sprite = getCom(SpriteRender.class);
		sprite.region = region;
		sprite.sortingOrder = sortingOrder;
		sprite.customShader = shader;
		return this;
	}
	
	public EntityBuilder Sprite(TextureRegion region,int sortingOrder){
		SpriteRender sprite = getCom(SpriteRender.class);
		sprite.region = region;
		sprite.sortingOrder = sortingOrder;
		return this;
	}
	//pendingDelete
	public EntityBuilder Delete(float delay){
		PendingDelete p = getCom(PendingDelete.class);
		p.delay = delay;
		return this;
	}
	//playerComp
	public EntityBuilder player(){
		//PlayerComp p = 
		getCom(PlayerComp.class);
		//后续可能有参数
		return this;
	}
	//Transform
	public EntityBuilder Pos(float x,float y){
		Transform t = getCom(Transform.class);
		t.setPosX(x); 
		t.setPosY(y); 
		return this;
	}
	public EntityBuilder Rotation(float x,float y){
		Transform t = getCom(Transform.class);
		t.setRotationX(x); 
		t.setRotationY(y); 
		return this;
	}
	public EntityBuilder Scale(float x,float y){
		Transform t = getCom(Transform.class);
		t.setScaleX(x); 
		t.setScaleY(y); 
		return this;
	}
	private <T extends Component> T getCom(Class<T> type){
		if(entity.getComponent(type) == null){
			return entity.edit().create(type);
		}
		return entity.getComponent(type);
	}
	
	public int getId(){
		return entity.getId();
	}
}

